package ua.at.tsvetkov.samizdat.fragments;

import ua.at.tsvetkov.samizdat.Const;
import ua.at.tsvetkov.samizdat.R;
import ua.at.tsvetkov.samizdat.activities.AcMainPhone;
import ua.at.tsvetkov.samizdat.activities.AcTextFlipper;
import ua.at.tsvetkov.samizdat.activities.IMenuActivity;
import ua.at.tsvetkov.samizdat.adapters.AdtBooks;
import ua.at.tsvetkov.util.Log;
import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;

public class FrgBooks extends Fragment {

	private ListView				fragment2listView;
	private AdtBooks				adapterBooks;

	private Integer				fragmentGenresClickedPosition;

	private Boolean				enableAnnotation		= false;

	private final static int	CURRENT_LIST_VIEW_ID	= 101;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		fragmentGenresClickedPosition = getArguments().getInt(Const.FRAGMENT_GENRES_CLICK_POSITION);
		getActivity().getIntent().removeExtra(Const.FRAGMENT_GENRES_CLICK_POSITION);
		View v = inflater.inflate(R.layout.frg_authors, container, false);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
		Boolean enableAnnotation = prefs.getBoolean("annotationPref", false);
		this.enableAnnotation = enableAnnotation;

		fragment2listView = new ListView(getActivity());
		fragment2listView.setDrawingCacheEnabled(false);
		fragment2listView.setScrollingCacheEnabled(false);
		fragment2listView.setId(CURRENT_LIST_VIEW_ID);
		fragment2listView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

		RelativeLayout parentView = (RelativeLayout) getView();
		parentView.addView(fragment2listView);

		fragment2listView.setOnItemClickListener(getFragment2ListViewOnItemClickListener());

		if (fragmentGenresClickedPosition != null) {
			adapterBooks = new AdtBooks(FrgBooks.this);
			adapterBooks.refreshListViewWithIdAndAnnotation(fragmentGenresClickedPosition, enableAnnotation);
			fragment2listView.setAdapter(adapterBooks);
		} else {
			adapterBooks = new AdtBooks(FrgBooks.this);
			adapterBooks.refreshListViewWithIdAndAnnotation(1, enableAnnotation);// if clicked position null than view first genre
			fragment2listView.setAdapter(adapterBooks);
		}

	}

	@Override
	public void onResume() {
		super.onResume();

		if (getActivity() instanceof AcMainPhone) {
			((IMenuActivity) getActivity()).showSortMenu();
		}

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
		Boolean enableAnnotation = prefs.getBoolean("annotationPref", false);
		if (this.enableAnnotation != enableAnnotation) {
			if (fragmentGenresClickedPosition != null) {
				adapterBooks.refreshListViewWithIdAndAnnotation(fragmentGenresClickedPosition, enableAnnotation);
			} else {
				adapterBooks.refreshListViewWithIdAndAnnotation(1, enableAnnotation);// if clicked position null than view first genre
			}
			this.enableAnnotation = enableAnnotation;
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		if (getActivity() instanceof AcMainPhone) {
			((IMenuActivity) getActivity()).hideSortMenu();
		}
	}

	private AdapterView.OnItemClickListener getFragment2ListViewOnItemClickListener() {
		return new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {
				// Cursor cursor = (Cursor) fragment2listView.getItemAtPosition(position);
				TextView currentTitleTextView = (TextView) v.findViewById(R.id.frg_books_list_view_item_title);
				// String title = cursor.getString(1);
				Intent clickIntent = new Intent(getActivity(), AcTextFlipper.class);
				clickIntent.putExtra(Const.FRAGMENT_BOOKS_CLICK_TITLE, currentTitleTextView.getText());
				getActivity().startActivity(clickIntent);
			}
		};
	}

	public void refreshListViewWithId(int id, Boolean enableAnnotation) {
		adapterBooks.refreshListViewWithIdAndAnnotation(id, enableAnnotation);
	}

	public void onDestroyView() {
		super.onDestroyView();
	}

	public AdtBooks getFragmentBooksAdapter() {
		return adapterBooks;
	}
}
